package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.wxcomment.WXcommentListVo;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.vo.wx.wxcomment.WXCommentCountVo;
import com.cskaoyan.service.wx.WXCommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :rain
 * @description
 * @since 2022/06/29 15:08
 */
@RestController
@RequestMapping("wx/comment")
public class WXCommentController {

    @Autowired
    WXCommentService commentService;

    /**
     * 请求参数   count?valueId=1109008&type=0
     *
     * @param comment 直接封装在对象中
     * @return
     */
    @RequestMapping("count")
    public BaseRespVo count(MarketComment comment) {
        WXCommentCountVo wxCommentCountVo = commentService.queryPicAndCommentCount(comment);
        return BaseRespVo.ok(wxCommentCountVo);
    }

    /**
     * 根据当前的valueId获取到商品的id
     * 这个showType=0就是包含有图和无图的评论，showType=1仅包含的是有图的评论
     *
     * @param comment valueId=1064007&type=0是商品的评论 type=1是商品的评论
     * @param info    limit=20&page=1
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo list(MarketComment comment, BasePageInfo info, Integer showType) {
        CommonData<WXcommentListVo> data = commentService.queryAllCommentInfo(comment, info, showType);
        return BaseRespVo.ok(data);
    }

    /**
     * 专题评论中有个post方法 专题评论的type为1 说明是专题评论
     *
     * @param comment 请求的对象,type为1
     * @return
     */
    @RequestMapping("post")
    public BaseRespVo post(@RequestBody MarketComment comment) {
        //获取当前正在登录的用户
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (principals == null){
            return BaseRespVo.error(501,"需要重新登陆");
        }
        MarketUser user = (MarketUser) principals.getPrimaryPrincipal();
        //MarketUser user = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        MarketComment marketComment = commentService.topicComment(comment, user);
        return BaseRespVo.ok(marketComment);
    }
}
